﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using GtsServer.DAL;
using System.Data;
using GtsServer.Common;
using GtsServer.Model;

namespace GtsServer.Service.Controller
{
    public class PrizeController
    {
        public PrizeController() { }

        public UseStageResponse UseStage(UseStageRequest request)
        {
            UseStageResponse response = new UseStageResponse()
            {
                bigevent = new UseStageManage(),
                result = 2,
                type = 0
            };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            PrizeDAL dal = new PrizeDAL();
            DataSet ds = dal.ReceiveStage(userAccount, request.id);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                response.result = Convert.ToInt32(ds.Tables[0].Rows[0]["result"].ToString());
                response.type = Convert.ToInt32(ds.Tables[0].Rows[0]["type"].ToString());
                response.bigevent.id = Convert.ToInt32(ds.Tables[0].Rows[0]["eventID"].ToString());
            }

            return response;
        }

        public NoticeClaimGoodsResponse NoticeClaimGoods(NoticeClaimGoodsRequest request)
        {
            NoticeClaimGoodsResponse response = new NoticeClaimGoodsResponse();

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            PrizeDAL dal = new PrizeDAL();
            DataSet ds = dal.NoticeClaimGoods(userAccount);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                response.result = Convert.ToInt32(ds.Tables[0].Rows[0]["result"].ToString());
            }
            return response;
        }

        public BuyGoodsResponse BuyGoods(BuyGoodsRequest request)
        {
            BuyGoodsResponse response = new BuyGoodsResponse();

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            PrizeDAL dal = new PrizeDAL();
            DataSet ds = dal.BuyGoods(userAccount, request.id);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                response.result = Convert.ToInt32(ds.Tables[0].Rows[0]["result"].ToString());
            }

            return response;
        }

        /// <summary>
        /// 获取金币界面列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public PrizeGoldResponse GetShopGoodsList(PrizeGoldRequest request)
        {
            PrizeGoldResponse response = new PrizeGoldResponse()
            {
                coin = new List<PrizeGoldManage>()
            };
            PrizeDAL dal = new PrizeDAL();
            DataSet ds = dal.GetListByIsShopShow(request.type);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    PrizeGoldManage model = new PrizeGoldManage()
                    {
                        id = Convert.ToInt32(item["ID"].ToString()),
                        tit = item["Name"].ToString(),
                        present = item["Remark"].ToString(),
                        money = Convert.ToDecimal(item["Price"].ToString()),
                    };
                    response.coin.Add(model);
                }
            }

            return response;
        }

        public EditGoodsStutasResponse EditGoodsStutas(EditGoodsStutasRequest request)
        {
            EditGoodsStutasResponse response = new EditGoodsStutasResponse() { result = 2 };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            UserPrizeDAL dal = new UserPrizeDAL();
            DataSet ds = dal.EditGoodsStutas(userAccount, request.id, request.stutas);

            if (ds.Tables[0].Rows.Count > 0)
            {
                response.result = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
            }

            return response;
        }

        public MyGoodsListResponse GetMyGoodsList(MyGoodsListRequest request)
        {
            MyGoodsListResponse response = new MyGoodsListResponse()
            {
                my_item_list = new List<MyGoodsListManage>()
            };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;
            UserPrizeDAL dal = new UserPrizeDAL();
            DataSet ds = dal.GetMyGoodsList(userAccount);

            foreach (DataRow item in ds.Tables[0].Rows)
            {
                MyGoodsListManage model = new MyGoodsListManage()
                {
                    id = Convert.ToInt32(item["id"].ToString()),
                    name = item["name"].ToString(),
                    image_url = CommonConfig.imgServiceAddr + CommonConfig.image + item["image_url"],
                    count = "数量：" + item["count"].ToString()
                };
                response.my_item_list.Add(model);
            }

            if (ds != null && ds.Tables.Count == 2 && ds.Tables[1].Rows.Count > 0)
                response.egg = "我的彩蛋：" + ds.Tables[1].Rows[0]["Prizecount"];
            else
                response.egg = "我的彩蛋：0";

            return response;
        }

        public ClaimGoodsListResponse GetClaimGoodsList(ClaimGoodsListRequest request)
        {
            ClaimGoodsListResponse response = new ClaimGoodsListResponse()
            {
                get_item_list = new List<ClaimGoodsListManage>()
            };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            PrizeTaskReocrdDAL dal = new PrizeTaskReocrdDAL();
            DataSet ds = dal.GetClaimList(userAccount);

            foreach (DataRow item in ds.Tables[0].Rows)
            {
                ClaimGoodsListManage model = new ClaimGoodsListManage()
                {
                    id = Convert.ToInt32(item["id"].ToString()),
                    type = Convert.ToInt32(item["type"].ToString()),
                    image_url = CommonConfig.imgServiceAddr + CommonConfig.image + item["image_url"],
                    represent = item["represent"].ToString(),
                    name = item["name"].ToString(),
                    matchid = string.IsNullOrEmpty(item["matchid"].ToString()) ? 0 : Convert.ToInt32(item["matchid"].ToString()),
                    coin = Convert.ToInt32(item["coin"].ToString())
                };
                response.get_item_list.Add(model);
            }

            if (ds != null && ds.Tables.Count == 2 && ds.Tables[1].Rows.Count > 0)
                response.egg = "我的彩蛋：" + ds.Tables[1].Rows[0]["Prizecount"];
            else
                response.egg = "我的彩蛋：0";

            return response;
        }
        /// <summary>
        /// 获取道具商城列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public GoodsListResponse GetGoodsList(GoodsListRequest request)
        {
            GoodsListResponse response = new GoodsListResponse()
            {
                item_list = new List<GoodsListManage>()
            };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            PrizeDAL dal = new PrizeDAL();
            DataSet ds = dal.GetListByIsShopShow(userAccount, "(" + Convert.ToInt32(EnumPrizeType.道具) + "," + Convert.ToInt32(EnumPrizeType.事件道具) + "," + Convert.ToInt32(EnumPrizeType.货币宝箱) + "," + Convert.ToInt32(EnumPrizeType.道具宝箱) + "," + Convert.ToInt32(EnumPrizeType.限时增强道具) + ")");
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow item in ds.Tables[0].Rows)
                    {
                        GoodsListManage model = new GoodsListManage()
                        {
                            id = Convert.ToInt32(item["ID"].ToString()),
                            name = "名称：" + item["Name"].ToString(),
                            money = Convert.ToDecimal(item["Price"].ToString()),
                            prize = "价格：" + item["ExchangeName"].ToString() + item["Price"].ToString(),
                            type = item["ExchangeName"].ToString().Equals("人民币") ? 1 : 2,
                            image_url = CommonConfig.imgServiceAddr + CommonConfig.image + item["Image"].ToString()
                        };
                        response.item_list.Add(model);
                    }
                }
                if (ds.Tables[1].Rows.Count > 0)
                    response.egg = "我的彩蛋：" + ds.Tables[1].Rows[0]["Prizecount"];
                else
                    response.egg = "我的彩蛋：0";
            }

            return response;
        }

        public PrizeDetailResponse GetPrizeDetail(PrizeDetailRequest request)
        {
            PrizeDetailResponse response = new PrizeDetailResponse();
            int id = request.id;
            PrizeDAL dal = new PrizeDAL();
            DataSet ds = dal.GetClientPrizeDetail(id);
            if (ds.Tables[0].Rows.Count > 0)
            {
                string prizeName = ds.Tables[0].Rows[0]["prizeName"].ToString();
                response = new PrizeDetailResponse()
                {
                    image_url = CommonConfig.imgServiceAddr + CommonConfig.image + ds.Tables[0].Rows[0]["Image"].ToString(),
                    name = "名称：" + ds.Tables[0].Rows[0]["Name"].ToString(),
                    price = "所需" + prizeName + "：" + ds.Tables[0].Rows[0]["Price"].ToString() + prizeName,
                    detail = "奖品详情：" + ds.Tables[0].Rows[0]["Remark"].ToString()
                };
            }
            return response;
        }

        private string StutasName(int stutasID)
        {
            string status = string.Empty;
            if (stutasID.Equals(Convert.ToInt32(EnumUserPrizeStuas.处理中)))
                status = EnumUserPrizeStuas.处理中.ToString();
            else if (stutasID.Equals(Convert.ToInt32(EnumUserPrizeStuas.拒绝)))
                status = EnumUserPrizeStuas.拒绝.ToString();
            else if (stutasID.Equals(Convert.ToInt32(EnumUserPrizeStuas.审核中)))
                status = EnumUserPrizeStuas.审核中.ToString();
            else if (stutasID.Equals(Convert.ToInt32(EnumUserPrizeStuas.已兑换)))
                status = EnumUserPrizeStuas.已兑换.ToString();
            return status;
        }

        public PrizeRecordDetialResponse ExchangePrizeDetail(PrizeRecordDetailRequest request)
        {
            PrizeRecordDetialResponse response = new PrizeRecordDetialResponse();
            PrizeRecordDAL dal = new PrizeRecordDAL();
            PrizeRecordShow model = dal.GetModel(request.record_id);

            string stutas = StutasName(model.State);
            string createHour = model.CreateTime.Hour.ToString().Length < 2 ? "0" + model.CreateTime.Hour.ToString() : model.CreateTime.Hour.ToString();
            string createMinite = model.CreateTime.Minute.ToString().Length < 2 ? "0" + model.CreateTime.Minute.ToString() : model.CreateTime.Minute.ToString();
            string editHour = model.EditTime.Hour.ToString().Length < 2 ? "0" + model.EditTime.Hour.ToString() : model.EditTime.Hour.ToString();
            string editMinite = model.EditTime.Minute.ToString().Length < 2 ? "0" + model.EditTime.Minute.ToString() : model.EditTime.Minute.ToString();

            response = new PrizeRecordDetialResponse()
            {
                order = "兑奖单号：" + model.OrderAccount,
                exchange_time = "兑换时间：" + model.CreateTime.Year + "年" + model.CreateTime.Month + "月" + model.CreateTime.Day + "日 " + createHour + ":" + createMinite,
                send_time = "发送时间：" + model.EditTime.Year + "年" + model.EditTime.Month + "月" + model.EditTime.Day + "日 " + editHour + ":" + editMinite,
                status = "兑换状态：" + stutas,
                name = "奖品名称：" + model.PrizeName
            };
            return response;
        }

        public ExchangePrizeResponse ExchangePrize(ExchangePrizeRequest request)
        {
            ExchangePrizeResponse response = new ExchangePrizeResponse()
            {
                userinfo = "uin_wrong"
            };
            PrizeDAL dal = new PrizeDAL();
            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;
            DataSet ds = dal.ExchangePrize(request.user.account, request.user.password, userAccount, request.user.id, request.user.name, request.user.qq, request.user.mphone, request.user.address);
            if (ds.Tables[0].Rows.Count > 0)
            {
                response.userinfo = ds.Tables[0].Rows[0][0].ToString();
            }

            return response;
        }

        public MyPrizeListResponse GetMyPrizeList(PrizeListRequest request)
        {
            MyPrizeListResponse response = new MyPrizeListResponse();
            response.my_prize_list = new List<MyPrizeListManage>();

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;
            PrizeDAL dal = new PrizeDAL();
            DataSet ds = dal.GetClientMyPrizeList(userAccount, Convert.ToInt32(EnumPrizeType.彩蛋));

            if (ds.Tables.Count == 2)
            {
                if (ds.Tables[0].Rows.Count > 0)
                    response.egg = "我的彩蛋：" + ds.Tables[0].Rows[0]["egg"].ToString();
                if (ds.Tables[1].Rows.Count > 0)
                {
                    List<MyPrizeModel> list = IListDataSet.DataSetToIList<MyPrizeModel>(ds, 1).ToList();
                    List<MyPrizeListManage> manageList = new List<MyPrizeListManage>();
                    foreach (var item in list)
                    {
                        string status = StutasName(item.status);

                        string hour = item.exchange_time.Hour.ToString().Length < 2 ? "0" + item.exchange_time.Hour.ToString() : item.exchange_time.Hour.ToString();
                        string minite = item.exchange_time.Minute.ToString().Length < 2 ? "0" + item.exchange_time.Minute.ToString() : item.exchange_time.Minute.ToString();
                        MyPrizeListManage model = new MyPrizeListManage()
                        {
                            record_id = item.record_id,
                            image_url = CommonConfig.imgServiceAddr + CommonConfig.image + item.image_url,
                            name = item.name,
                            status = status,
                            exchange_time = item.exchange_time.Year + "年" + item.exchange_time.Month + "月" + item.exchange_time.Day + "日 " + hour + ":" + minite
                        };
                        manageList.Add(model);
                    }
                    response.my_prize_list = manageList;
                }
            }

            return response;
        }

        public PrizeListResponse GetPrizeList(PrizeListRequest request)
        {
            PrizeListResponse response = new PrizeListResponse();
            response.user = new UserManage();
            response.prize_list = new List<PrizeList>();
            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;
            PrizeDAL dal = new PrizeDAL();
            DataSet ds = dal.GetClientPrizeList(userAccount, Convert.ToInt32(EnumPrizeType.奖品), Convert.ToInt32(EnumPrizeType.彩蛋));

            if (ds.Tables.Count == 3)
            {
                if (ds.Tables[0].Rows.Count > 0)
                    response.egg = "我的彩蛋：" + ds.Tables[0].Rows[0]["egg"].ToString();
                if (ds.Tables[1].Rows.Count > 0)
                {
                    response.user = new UserManage()
                    {
                        account = ds.Tables[1].Rows[0]["account"].ToString(),
                        password = ds.Tables[1].Rows[0]["password"].ToString()
                    };
                }
                if (ds.Tables[2].Rows.Count > 0)
                {
                    List<PrizeList> list = IListDataSet.DataSetToIList<PrizeList>(ds, 2).ToList();
                    foreach (var item in list)
                    {
                        item.image_url = CommonConfig.imgServiceAddr + CommonConfig.image + item.image_url;
                        //item.image_url = item.image_url.Replace("/","//");
                    }
                    response.prize_list = list;
                }
            }

            return response;
        }
    }
}
